package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/**
 * @ClassName ComboBoxDemo
 * @Description
 * @Author wangss
 * @date 2020.05.25 11:45
 * @Version 1.0
 */
public class ComboBox2Demo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        Button button = new Button("点击");

        Student s1 = new Student("小a", 12, 99d);
        Student s2 = new Student("小b", 18, 63d);
        Student s3 = new Student("小c", 20, 60d);
        Student s4 = new Student("小d", 20, 60d);
        Student s5 = new Student("小e", 20, 60d);

        ComboBox<Student> comboBox = new ComboBox<>();
        comboBox.getItems().addAll(s1, s2, s3, s4, s5);

        // 可以编辑
        comboBox.setEditable(true);
        // 文本提示
        comboBox.setPromptText("请输入内容");
        // 占位符，里边没有元素的时候起作用
//        comboBox.setPlaceholder(new Button("我是占位按钮"));

        // 设置默认显示多少行
//        comboBox.setVisibleRowCount(3);

        // 这里和comboBox.setEditable(true)互斥
        comboBox.setConverter(new StringConverter<Student>() {
            @Override
            public String toString(Student object) {
                if (object == null) {
                    return "";
                }

                for (Student item : comboBox.getItems()) {
                    if (item.getName().equals(object.getName())) {
                        return object.getName();
                    }
                }

                comboBox.getItems().add(object);

                return object.getName();
            }

            @Override
            public Student fromString(String string) {
                if (string == null || "".equals(string)) {
                    return null;
                }

                System.out.println(string);
                // 这里接受列表中输入数据
                Student s = new Student(string, 14, 124d);
                return s;
            }
        });

        AnchorPane.setTopAnchor(comboBox, 10d);
        AnchorPane.setLeftAnchor(comboBox, 10d);
        AnchorPane.setTopAnchor(button, 10d);
        AnchorPane.setLeftAnchor(button, 260d);
        anchorPane.getChildren().addAll(comboBox, button);

        primaryStage.setScene(scene);
        primaryStage.setTitle("combobox");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        comboBox.getSelectionModel().selectedIndexProperty().addListener((observable, oldValue, newValue) -> {

        });

        comboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {

        });
    }
}
